package com.smartsofa.util;

import java.util.ArrayList;

import android.R.integer;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.Region;
import android.graphics.drawable.NinePatchDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.MeasureSpec;

import com.smartsofa.R;
import com.smartsofa.util.LineView.Dot;

public class DayLineView extends View{
	 
	private int mViewHeight;
	 private boolean autoSetDataOfGird = true;
	 private boolean autoSetGridWidth = true;
	 private boolean isDrawDataLine = true;
	 public boolean showPopup = true; 
	   
	//for different style
	    private int styleMode=1;
	    private float mMax;
	    private float mMin;
	//datalist
	    private ArrayList<String> bottomTextList = new ArrayList<String>();
	    
	    private ArrayList<ArrayList<Float>> dataLists;
	    private ArrayList<Float> dataList;
	    
	    private ArrayList<Float> xCoordinateList = new ArrayList<Float>();
	    private ArrayList<Float> yCoordinateList1 = new ArrayList<Float>();
	    private ArrayList<Float> yCoordinateList2 = new ArrayList<Float>();
	    private ArrayList<ArrayList<Float>> yCoordinateLists = new ArrayList<ArrayList<Float>>();
	    
	    private ArrayList<ArrayList<Dot>> drawDotLists = new ArrayList<ArrayList<Dot>>();
	    private ArrayList<Dot> drawDotList = new ArrayList<Dot>();
	    
	    //paint
	    private Paint bottomTextPaint = new Paint();
	    private Paint popupTextPaint = new Paint();
	    
	    //space
	    private int dataOfAGird = 10;
	    private int bottomTextHeight = 0;
	    private final int bottomTriangleHeight = 12;
	    //private int bottomTextDescent;
	    private int topLineLength = MyUtils.dip2px(getContext(), 12);; 
	    private int sideLineLength = MyUtils.dip2px(getContext(),45)/3*2;       
	    private int backgroundGridWidth = MyUtils.dip2px(getContext(),45);	    
	    private final int popupTopPadding = MyUtils.dip2px(getContext(),2);
	    private final int popupBottomMargin = MyUtils.dip2px(getContext(),5);
	    private final int bottomTextTopMargin = MyUtils.sp2px(getContext(),5);
	    private final int bottomLineLength = MyUtils.sp2px(getContext(), 22);
	    private final int DOT_INNER_CIR_RADIUS = MyUtils.dip2px(getContext(), 2);
	    private final int DOT_OUTER_CIR_RADIUS = MyUtils.dip2px(getContext(),5);
	    private final int MIN_TOP_LINE_LENGTH = MyUtils.dip2px(getContext(),12);
	    private final int MIN_VERTICAL_GRID_NUM = 4;
	    private final int MIN_HORIZONTAL_GRID_NUM = 1;
	    private final int bottomTextDescent = MyUtils.dip2px(getContext(),4);
	    
	    //color
	    private final int BACKGROUND_LINE_COLOR = Color.parseColor("#EEEEEE");
	    private final int BOTTOM_TEXT_COLOR = Color.parseColor("#9B9A9B");
	    private String[] colorArray = {"#e74c3c","#2980b9","#1abc9c"};
	    
	    //index of x coordinate
	    private ArrayList<Integer> timeIndex=new ArrayList<Integer>();
	    
	    
	    //popupwindow state
	    public static final int SHOW_POPUPS_All = 1;
	    public static final int SHOW_POPUPS_MAXMIN_ONLY = 2;
	    public static final int SHOW_POPUPS_NONE = 3;
	    private int showPopupType =SHOW_POPUPS_NONE ;
	    public void setShowPopup(int popupType) {
			this.showPopupType = popupType;
		}
	    private int[] popupColorArray = {R.drawable.popup_red,R.drawable.popup_blue,R.drawable.popup_green};
	    
	    //Dot
	    private Dot pointToSelect;
		private Dot selectedDot;
		
		 // onDraw optimisations
	    private final Point tmpPoint = new Point();
	    
	    private Runnable animator = new Runnable() {
	        @Override
	        public void run() {
	            boolean needNewFrame = false;
	            for(ArrayList<Dot> data : drawDotLists){
	            	for(Dot dot : data){
	                    dot.update();
	                    if(!dot.isAtRest()){
	                        needNewFrame = true;
	                    }
	                }
	            }
	            if (needNewFrame) {
	                postDelayed(this, 25);
	            }
	            invalidate();
	        }
	    };
	    
	    
	    
	    public DayLineView(Context context) {
			this(context,null);
			
		}
	    public DayLineView(Context context, AttributeSet attrs) {
	    	super(context, attrs);
	    	
	    	 popupTextPaint.setAntiAlias(true);
	         popupTextPaint.setColor(Color.WHITE);
	         popupTextPaint.setTextSize(MyUtils.sp2px(getContext(), 13));
	         popupTextPaint.setStrokeWidth(5);
	         popupTextPaint.setTextAlign(Paint.Align.CENTER);

	         bottomTextPaint.setAntiAlias(true);
	         bottomTextPaint.setTextSize(MyUtils.sp2px(getContext(),12));
	         bottomTextPaint.setTextAlign(Paint.Align.CENTER);
	         bottomTextPaint.setStyle(Paint.Style.FILL);
	         bottomTextPaint.setColor(BOTTOM_TEXT_COLOR);
		}
	    public void setBottomTextList(ArrayList<String> bottomTextList){
	    	if(bottomTextList.size()==0){
	    		for(int i=0;i<10;i++){
	        		timeIndex.add(1);
	        	}
	    	}else{
	    		this.dataList = null;
	            this.bottomTextList = bottomTextList;
	            int hour,min,sec;
		    	for(String s :bottomTextList){
		    		hour = Integer.valueOf(s.substring(0, 2));
		    		min = Integer.valueOf(s.substring(3, 5));
		    		sec =Integer.valueOf(s.substring(6, 8));
		    		timeIndex.add(hour*3600+min*60+sec);
		    		Log.i("timestring", s);
		    		Log.i("timeIndex",timeIndex.get(0)+"");
		    		
		    		}
		    	refreshXCoordinateList();
	    	}	 
	    	Rect r = new Rect();
	        String string="00:00";
	        bottomTextPaint.getTextBounds(string,0,string.length(),r);
	        bottomTextHeight = r.height();
	    }
	    public void setDataLists(ArrayList<ArrayList<Float>> dataLists){
	    	if(dataLists.size() == 0){
	        	for(int i=0;i<10;i++){
	        		dataList.add((float) 0);
	        	}
	        	dataLists.add(dataList);
	        }
	        else{
	        	selectedDot = null;
	            this.dataLists = dataLists;
	            yCoordinateLists.add(yCoordinateList1);
	            yCoordinateLists.add(yCoordinateList2);
	        }
	        for(ArrayList<Float> list : dataLists){        	
	        	if(list.size() > bottomTextList.size()){
	        		System.out.println("list.size():"+list.size()+"bottomTextList.size():"+bottomTextList.size());
	                throw new RuntimeException("dacer.LineView error:" +
	                        " dataList.size() > bottomTextList.size() !!!");
	            }
	        }
	        float biggestData = 0;
	        for(ArrayList<Float> list : dataLists){
	        	if(autoSetDataOfGird){
	                for(Float i:list){
	                    if(biggestData<i){
	                        biggestData = i;
	                    }
	                }
	        	}
	        	dataOfAGird = 1;
	        	while(biggestData/10 > dataOfAGird){
	        		dataOfAGird *= 10;
	        	}
	        }
	        
	        refreshAfterDataChanged();
	        showPopup = true;
	        setMinimumWidth(0); // It can help the LineView reset the Width,
	                                // I don't know the better way..
	        postInvalidate();
	    }
	    
	    private void refreshAfterDataChanged(){
	        float verticalGridNum = getVerticalGridlNum();
	        refreshTopLineLength(verticalGridNum);
	        refreshYCoordinateList(verticalGridNum);
	        refreshXCoordinateList();
	        refreshDrawDotList(verticalGridNum);
	    }
	    private float getVerticalGridlNum(){
	        float verticalGridNum;
	        switch(styleMode){
	        case 1: 
	        	mMax=200;
	        	mMin=30;break;
	        case 2: 
	        	mMax=12;
	        	mMin=3;break;
	        case 3: 
	        	mMax=100;
	        	mMin=94;break;
	        }
	        verticalGridNum=mMax-mMin;
	        return verticalGridNum;
	    }
	   
	    private void refreshXCoordinateList(){
	    	xCoordinateList.clear();
	    	float viewWidth = 12*backgroundGridWidth;
	    	for(int i=0;i<timeIndex.size();i++){
	    		xCoordinateList.add(sideLineLength+(timeIndex.get(i)/86400f)*viewWidth);
	    		Log.i("x location",""+xCoordinateList.get(i));
	    	}
	    }
	    private void refreshYCoordinateList(float verticalGridNum){
	        yCoordinateList1.clear();
	        yCoordinateList2.clear();
	        for(int k=0;k<dataLists.size();k++){
	        	
	        for(int i=0;i<dataLists.get(k).size();i++){        	
	            yCoordinateLists.get(k).add(topLineLength +
	                    ((mViewHeight-topLineLength-bottomTextHeight-bottomTextTopMargin-
	                            bottomLineLength-bottomTextDescent)*(1-(dataLists.get(k).get(i)-mMin)/(verticalGridNum))));
	            
	        }
	       
	        }
	    }
	    private void refreshDrawDotList(float verticalGridNum){
	        if(dataLists != null && !dataLists.isEmpty()){
	    		if(drawDotLists.size() == 0){
	    			for(int k = 0; k < dataLists.size(); k++){
	    				drawDotLists.add(new ArrayList<DayLineView.Dot>());
	    			}
	    		}
	        	for(int k = 0; k < dataLists.size(); k++){
	        		int drawDotSize = drawDotLists.get(k).isEmpty()? 0:drawDotLists.get(k).size();
	        		
	        		for(int i=0;i<dataLists.get(k).size();i++){
	                    float x = xCoordinateList.get(i);
	                    float y = yCoordinateLists.get(k).get(i);
	                    if(i>drawDotSize-1){
	                    	//闇冨嫶濮兼顒囧Б闊搁附銈� 鏂兼棄鐨滈爟婊婂珶.
	                        drawDotLists.get(k).add(new Dot(i,x, 0.0f, x, y, dataLists.get(k).get(i),k));
	                    }else{
	                    	//闇冨嫶濮兼顒囧Б闊歌兂妫� 闊拷鐡寸喛娼� 闈广倢鐖奸爟婊婂珶.
	                        drawDotLists.get(k).set(i, drawDotLists.get(k).get(i).setTargetData(x,y,dataLists.get(k).get(i),k));
	                    }
	                }
	        		
	        		int temp = drawDotLists.get(k).size() - dataLists.get(k).size();
	        		for(int i=0; i<temp; i++){
	        			drawDotLists.get(k).remove(drawDotLists.get(k).size()-1);
	        		}
	        	}
	        }
	        removeCallbacks(animator);
	        post(animator);
	    }
	    private void refreshTopLineLength(float verticalGridNum){
	        // For prevent popup can't be completely showed when backgroundGridHeight is too small.
	        // But this code not so good.
	        if((mViewHeight-topLineLength-bottomTextHeight-bottomTextTopMargin)/
	                (verticalGridNum+2)<getPopupHeight()){
	            topLineLength = getPopupHeight()+DOT_OUTER_CIR_RADIUS+DOT_INNER_CIR_RADIUS+2;
	        }else{
	            topLineLength = MIN_TOP_LINE_LENGTH;
	        }
	    }
	    @Override
	    protected void onDraw(Canvas canvas) {
	    	// TODO Auto-generated method stub
	    	super.onDraw(canvas);
	    	
	         if(bottomTextList.size()!=0 && dataLists.size()!=0 && dataLists.get(0).size()!=0){
	         if(isDrawDataLine == true){
	         drawStandardLines(canvas);      
	         drawDots(canvas);
	         if(dataLists.get(0).size()>1){
	         	drawLines(canvas);
	         	}
	         }
	         
	         if(showPopup && selectedDot != null){
	             drawPopup(canvas,
	                     String.valueOf(selectedDot.data),
	                     selectedDot.setupPoint(tmpPoint),popupColorArray[selectedDot.linenumber%3]);
	         }
	         }
	         drawCoordinateLine(canvas);
	         drawBackgroundLines(canvas);
	    }
	    /**
	     *
	     * @param canvas  The canvas you need to draw on.
	     * @param point   The Point consists of the x y coordinates from left bottom to right top.
	     *                Like is
	     *                
	     *                3
	     *                2
	     *                1
	     *                0 1 2 3 4 5
	     */
	    //鐢荤偣
	    private void drawDots(Canvas canvas){
	        Paint bigCirPaint = new Paint();
	        bigCirPaint.setAntiAlias(true);
	        Paint smallCirPaint = new Paint(bigCirPaint);
	        smallCirPaint.setColor(Color.parseColor("#FFFFFF"));
	     
	        if(drawDotLists!=null && !drawDotLists.isEmpty()){
	        	for(int k=0; k < drawDotLists.size(); k++){	
	        		bigCirPaint.setColor(Color.parseColor(colorArray[k%3]));
	        		for(Dot dot : drawDotLists.get(k)){
	                	canvas.drawCircle(dot.x,dot.y,DOT_OUTER_CIR_RADIUS,bigCirPaint);
	                	canvas.drawCircle(dot.x,dot.y,DOT_INNER_CIR_RADIUS,smallCirPaint);
	     
						if(styleMode==1&&((dot.data>140||dot.data<90)&&k==0)||((dot.data>90||dot.data<60)&&k==1)){
							canvas.drawText(Float.toString(dot.data), dot.x-7, dot.y-10, bigCirPaint);       		
					    }
	            	}
	        		
	        	}
	        }
	    }
	    
	    private void drawPopup(Canvas canvas,String num, Point point,int PopupColor){
	        boolean singularNum = (num.length() == 1);
	        int sidePadding = MyUtils.dip2px(getContext(),singularNum? 8:5);
	        int x = point.x;
	        int y = point.y-MyUtils.dip2px(getContext(),5);
	        Rect popupTextRect = new Rect();
	        popupTextPaint.getTextBounds(num,0,num.length(),popupTextRect);
	        Rect r = new Rect(x-popupTextRect.width()/2-sidePadding,
	                y - popupTextRect.height()-bottomTriangleHeight-popupTopPadding*2-popupBottomMargin,
	                x + popupTextRect.width()/2+sidePadding,
	                y+popupTopPadding-popupBottomMargin);

	        NinePatchDrawable popup = (NinePatchDrawable)getResources().getDrawable(PopupColor);
	        popup.setBounds(r);
	        popup.draw(canvas);
	        canvas.drawText(num, x, y-bottomTriangleHeight-popupBottomMargin, popupTextPaint);
	    }

	    private int getPopupHeight(){
	        Rect popupTextRect = new Rect();
	        popupTextPaint.getTextBounds("9",0,1,popupTextRect);
	        Rect r = new Rect(-popupTextRect.width()/2,
	                 - popupTextRect.height()-bottomTriangleHeight-popupTopPadding*2-popupBottomMargin,
	                 + popupTextRect.width()/2,
	                +popupTopPadding-popupBottomMargin);
	        return r.height();
	    }
	  //鐢荤嚎
	    private void drawLines(Canvas canvas){
	        Paint linePaint = new Paint();
	        linePaint.setAntiAlias(true);
	        linePaint.setStrokeWidth(MyUtils.dip2px(getContext(), 2));
	        for(int k = 0; k<drawDotLists.size(); k ++){
	        	linePaint.setColor(Color.parseColor(colorArray[k%3]));
		        for(int i=0; i<drawDotLists.get(k).size()-1; i++){
		            canvas.drawLine(drawDotLists.get(k).get(i).x,
		                    drawDotLists.get(k).get(i).y,
		                    drawDotLists.get(k).get(i+1).x,
		                    drawDotLists.get(k).get(i+1).y,
		                    linePaint);
		        }
	        }
	    }
	  //鐢诲潗鏍囩嚎
	    private void drawCoordinateLine(Canvas canvas) {
	    	float verticalGridNum = getVerticalGridlNum();
	    	float delta = verticalGridNum/5;
	    	Paint linePaint = new Paint();
	    	 linePaint.setAntiAlias(true);
	         linePaint.setStyle(Paint.Style.STROKE);
	         linePaint.setStrokeWidth(MyUtils.dip2px(getContext(), 1f));
	         linePaint.setColor(Color.parseColor("#7f7f7f"));
	         int coordinateWidth = MyUtils.dip2px(getContext(), 6);
	        
	         canvas.drawLine(sideLineLength,
	        		 mViewHeight - bottomTextTopMargin - bottomTextHeight-bottomTextDescent,
	        		 sideLineLength+backgroundGridWidth*12, 
	        		 mViewHeight - bottomTextTopMargin - bottomTextHeight-bottomTextDescent,linePaint);
	         canvas.drawLine(sideLineLength,
	                 0,
	                 sideLineLength,
	                 mViewHeight - bottomTextTopMargin - bottomTextHeight-bottomTextDescent,
	                 linePaint);
	         //draw vertical coordinate
	         for(int i=0;i<6;i++){
	        	 float y=topLineLength +
	                     ((mViewHeight-topLineLength-bottomTextHeight-bottomTextTopMargin-
	                             bottomLineLength-bottomTextDescent)*(1-( mMax-i*delta-mMin)/(verticalGridNum)));
	        	 canvas.drawLine(sideLineLength,
	        			 y,
	        			 sideLineLength+coordinateWidth, 
	        			 y, linePaint);  
	        	 y=topLineLength +
	                ((mViewHeight-topLineLength-bottomTextHeight-bottomTextTopMargin-
	         bottomLineLength-bottomTextDescent)*(1-( mMax-i*delta-mMin)/(verticalGridNum)))+MyUtils.dip2px(getContext(),5);
	        	 
	             float x=sideLineLength+coordinateWidth*4;
	             canvas.drawText(""+(mMax-i*delta), x, y, bottomTextPaint);
	             
	         }
	         //draw horizontal coordinate
	         
	         
	         for(int i=0;i<5;i++){
	        	 float x = sideLineLength+i*3*backgroundGridWidth;
	        	 canvas.drawText(""+i*6+":00", x, 
	        			 mViewHeight - bottomTextHeight*0.5f-bottomTextDescent, bottomTextPaint);
	        	 
	         }
	    }
	    
	  //鐢绘爣鍑嗙嚎
	    private void drawStandardLines(Canvas canvas){
	        Paint linePaint = new Paint();        
	        float verticalGridNum = getVerticalGridlNum();
	        linePaint.setAntiAlias(true);
	        linePaint.setStyle(Paint.Style.STROKE);
	        linePaint.setStrokeWidth(MyUtils.dip2px(getContext(), 1f));
	        linePaint.setColor(Color.parseColor("#32CD32"));
	        PathEffect effects = new DashPathEffect(
	                new float[]{10,5,10,5}, 1);
	        linePaint.setPathEffect(effects);
	        switch(styleMode){
	        case 1:
	        	int[] standard_value1={60,90,140};
	            for(int i=0;i<3;i++){
	            	float y=topLineLength +
	                        ((mViewHeight-topLineLength-bottomTextHeight-bottomTextTopMargin-
	                                bottomLineLength-bottomTextDescent)*(1-( standard_value1[i]-mMin)/(verticalGridNum)));
	            	
	    			canvas.drawLine(sideLineLength,y,
	    					sideLineLength+backgroundGridWidth*12,
	    					y, linePaint);
	            }
	        	break;
	        case 2:
	        	float[] standard_value2={3.9f,11.1f};
	            for(int i=0;i<2;i++){
	            	float y=topLineLength +
	                        ((mViewHeight-topLineLength-bottomTextHeight-bottomTextTopMargin-
	                                bottomLineLength-bottomTextDescent)*(1-( standard_value2[i]-mMin)/(verticalGridNum)));
	    			canvas.drawLine(sideLineLength,y,
	    					sideLineLength+backgroundGridWidth*12,
	    					y, linePaint);
	            }
	        	break;
	        case 3:
	        	int[] standard_value3={95,98};
	            for(int i=0;i<2;i++){
	            	float y=topLineLength +
	                        ((mViewHeight-topLineLength-bottomTextHeight-bottomTextTopMargin-
	                                bottomLineLength-bottomTextDescent)*(1-( standard_value3[i]-mMin)/(verticalGridNum)));
	            	canvas.drawLine(sideLineLength,y,
	            			sideLineLength+backgroundGridWidth*12,
	    					y, linePaint);
	            }
	        	break;
	        }
	       
	       
	    }
	    
	    private void drawBackgroundLines(Canvas canvas){
	        Paint paint = new Paint();
	        paint.setStyle(Paint.Style.STROKE);
	        paint.setStrokeWidth(MyUtils.dip2px(getContext(),1f));
	        paint.setColor(BACKGROUND_LINE_COLOR);
	        PathEffect effects = new DashPathEffect(
	                new float[]{10,5,10,5}, 1);

	        //draw vertical lines
	        for(int i=1;i<5;i++){
	        	float x=sideLineLength+i*3*backgroundGridWidth;
	            canvas.drawLine(x,
	                    0,
	                    x,
	                    mViewHeight - bottomTextTopMargin - bottomTextHeight-bottomTextDescent,
	                    paint);
	        }
	    }
	    @Override
	    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
	        int mViewWidth = measureWidth(widthMeasureSpec);
	        mViewHeight = measureHeight(heightMeasureSpec);
//	        mViewHeight = MeasureSpec.getSize(measureSpec);
//	        refreshAfterDataChanged();
	        setMeasuredDimension(mViewWidth,mViewHeight);
	    }

	    private int measureWidth(int measureSpec){
	        int horizontalGridNum = 12;
	        int preferred = backgroundGridWidth*horizontalGridNum+sideLineLength*2;
	        return getMeasurement(measureSpec, preferred);
	    }

	    private int measureHeight(int measureSpec){
	        int preferred = 0;
	        return getMeasurement(measureSpec, preferred);
	    }

	    private int getMeasurement(int measureSpec, int preferred){
	        int specSize = MeasureSpec.getSize(measureSpec);
	        int measurement;
	        switch(MeasureSpec.getMode(measureSpec)){
	            case MeasureSpec.EXACTLY:
	                measurement = specSize;
	                break;
	            case MeasureSpec.AT_MOST:
	                measurement = Math.min(preferred, specSize);
	                break;
	            default:
	                measurement = preferred;
	                break;
	        }
	        return measurement;
	    }

	    
	    class Dot{
	        float x;
	        float y;
	        float data;
	        float targetX;
	        float targetY;
	        int linenumber;
	        int index;
	        int velocity = MyUtils.dip2px(getContext(),18);

	        Dot(int index,float x,float y,float targetX,float targetY,Float data,int linenumber){
	        	this.index = index;
	            this.x = x;
	            this.y = y;
	            this.linenumber = linenumber;
	            setTargetData(targetX, targetY,data,linenumber);
	        }

	        Point setupPoint(Point point) {
	            point.set((int)x, (int) y);
	            return point;
	        }

	        Dot setTargetData(float targetX,float targetY,Float data,int linenumber){
	            this.targetX = targetX;
	            this.targetY = targetY;
	            this.data = data;
	            this.linenumber = linenumber;
	            return this;
	        }

	        boolean isAtRest(){
	            return (x==targetX)&&(y==targetY);
	        }

	        void update(){
	            x = updateSelf(x, targetX, velocity);
	            y = updateSelf(y, targetY, velocity);
	        }

	        
	        private float updateSelf(float origin, float target, int velocity){
	            if (origin < target) {
	                origin += velocity;
	            } else if (origin > target){
	                origin-= velocity;
	            }
	            if(Math.abs(target-origin)<velocity){
	                origin = target;
	            }
	            return origin;
	        }
	    }
	    @Override
	    public boolean onTouchEvent(MotionEvent event) {
	    	if (event.getAction() == MotionEvent.ACTION_DOWN) {
	            pointToSelect = findPointAt((int) event.getX(), (int) event.getY());
	        } else if (event.getAction() == MotionEvent.ACTION_UP) {
	            if (pointToSelect != null) {
	                selectedDot = pointToSelect;
	                pointToSelect = null;
	                postInvalidate();
	            }
	        }
	    	return true;
	    }
	    private Dot findPointAt(int x, int y) {
	        if (drawDotLists.isEmpty()) {
	            return null;
	        }

	        final int width = backgroundGridWidth/2;
	        final Region r = new Region();

	        for (ArrayList<Dot> data : drawDotLists) {
	            for (Dot dot : data) {
	                final int pointX = (int)dot.x;
	                final int pointY = (int) dot.y;

	                r.set(pointX - width, pointY - width, pointX + width, pointY + width);
	                if (r.contains(x, y)){
	                    return dot;
	                }
	            }
	        }

	        return null;
	    }
	    public void setStyleMode(int styleMode){
	    	this.styleMode=styleMode;
	    }
	    public int getStyleMode(){
	    	return this.styleMode;
	    }
	    public void setDrawDataLineState( Boolean ifornot){
	    	this.isDrawDataLine = ifornot;
	    }
	    public Boolean getDrawDataLineState(){
	    	return this.isDrawDataLine;
	    }
}
